/**
 *    Copyright 2012 Geensoft S.A.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **/
package ar.com.greensoft.neosystem.dao.jpa;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.NoResultException;
import javax.persistence.NonUniqueResultException;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import ar.com.greensoft.neosystem.dao.NoSuchEntityException;
import ar.com.greensoft.neosystem.dao.PaisDao;
import ar.com.greensoft.neosystem.model.address.Pais;
import ar.com.greensoft.neosystem.model.address.Pais_;
import ar.com.greensoft.neosystem.model.references.ISO3166_1;
import ar.com.greensoft.neosystem.search.SearchFilter;
import ar.com.greensoft.neosystem.search.TextSearchFilter;
import ar.com.greensoft.neosystem.util.SearchUtil;

/**
 * 
 * @author Jorge E. Villaverde
 * @since 25/01/2012
 * @version 1.0.0
 */
@Stateless
public class JpaPaisDaoImpl 
	extends AbstractLongIdJpaDaoImpl<Pais> 
	implements PaisDao {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public List<Pais> findLikeName(String name) {
		logger.trace("Returning the list of " + getEntityClazz().getSimpleName() + " with name like: " + name);
		
		TypedQuery<Pais> query = 
				getEntityManager().createNamedQuery(Pais.FIND_LIKE_NAME_QUERY_NAME, Pais.class);

		query.setParameter("name", name);

		return query.getResultList();
	}

	@Override
	public Pais findByCode(ISO3166_1 codigoISO) throws NoSuchEntityException {
		logger.trace("Returning the list of " + getEntityClazz().getSimpleName() + " with ISO CODE: " + codigoISO);
		
		TypedQuery<Pais> query = 
				getEntityManager().createNamedQuery(Pais.FIND_BY_ISO_QUERY_NAME, Pais.class);

		query.setParameter("codigoISO", codigoISO);

		try {
			return query.getSingleResult();
		} catch (NoResultException e) {
		} catch (NonUniqueResultException e) {
		}
		return null;
	}

	@Override
	protected Class<Pais> getEntityClazz() {
		return Pais.class;
	}

	@Override
	protected String getFindByNameNamedQuery() {
		return Pais.FIND_BY_NAME_QUERY_NAME;
	}
	
	@Override
	protected Expression<Boolean> createWhereFromSearchFilter(SearchFilter searchFilter, Root<Pais> root) {
		if(searchFilter == null || !searchFilter.hasFilter())
			return null;
		
		TextSearchFilter tsf = (TextSearchFilter)searchFilter;
		String searchText = SearchUtil.likeString(tsf.getText());
		Predicate predicate = getEntityManager().getCriteriaBuilder().like(
				root.get(Pais_.name),
				searchText); 
		return predicate;
	}}
